
# coding:utf-8
from lib import al
from math import sqrt


def dir2vec(_dir, scale=1):
    value = [0, 0]
    if _dir == al.KEY_LEFT:
        value[0] -= 1*scale
    elif _dir == al.KEY_RIGHT:
        value[0] += 1*scale
    elif _dir == al.KEY_UP:
        value[1] -= 1*scale
    elif _dir == al.KEY_DOWN:
        value[1] += 1*scale
    return value

def dirs2vec(dirs, scale=1):
    value = [0, 0]
    for _dir in dirs:
        if _dir == al.KEY_LEFT:
            value[0] -= 1*scale
        elif _dir == al.KEY_RIGHT:
            value[0] += 1*scale
        elif _dir == al.KEY_UP:
            value[1] -= 1*scale
        elif _dir == al.KEY_DOWN:
            value[1] += 1*scale
    return value

def dir2vecx(_dir, scalex=1, scaley=1):
    value = [0, 0]
    if _dir == al.KEY_LEFT:
        value[0] -= 1*scalex
    elif _dir == al.KEY_RIGHT:
        value[0] += 1*scalex
    elif _dir == al.KEY_UP:
        value[1] -= 1*scaley
    elif _dir == al.KEY_DOWN:
        value[1] += 1*scaley
    return value

vec_add = lambda a,b : tuple([x+y for x,y in zip(a,b)])
def vec_dist(a,b):
    return sqrt(pow(a.x-b.x, 2) + pow(a.y-b.y, 2))

# 将传入的value限制在两个区间内
def limit(value, min, max):
    if value < min:
        value = min
    elif value > max:
        value = max
    return value
